package com.wwwarehouse.contract.contract.modify;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.contract.adapter.ContractChooseGoodsAdapter;
import com.wwwarehouse.contract.adapter.SelectGoodsAdapter;
import com.wwwarehouse.contract.bean.ContractChooseGoodsBean;
import com.wwwarehouse.contract.bean.ResourceItemBean;
import com.wwwarehouse.contract.contract.TheParentFragment;
import com.wwwarehouse.contract.core.ContractConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import contract.wwwarehouse.com.contract.R;

/**
 * @author Joseph on 2017/10/18.
 *         <p/>
 *         商品列表
 */

public class ChooseGoodsListFragment extends TheParentFragment {

    private RecyclerView mRvContent;
    private ArrayList<ContractChooseGoodsBean.ListBean> tagBeanList = new ArrayList<>();
    ContractChooseGoodsAdapter mAdapter;
    private int num;
    private int pos;
    private String mSort;
    private String buId;
    private String mContractUkid;
    private List<ContractChooseGoodsBean.ListBean> selectTag;
    private List<ContractChooseGoodsBean.ListBean> mListBean = new ArrayList<>();


    @Override
    protected int getContentId() {
        return R.layout.release_object_list;
    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        return null;
    }

    /**
     * bundle.putString("supplierBusinessId", mSupplierBusinessId); //   业务单位ID(必填)
     bundle.putString("contractUkid", mContractUkid); //  发布ukid
     bundle.putString("rsName", ""); // 资源名称
     bundle.putString("sort", mSort); //     排序(DESC,ASC)
     bundle.putSerializable("selectTag", (Serializable) selectTag);
     */
    @Override
    protected void loadDatas() {
        if (getArguments() == null || getArguments().getString("supplierBusinessId") == null
                || getArguments().getString("contractUkid") == null
                || getArguments().getSerializable("data") == null) {
            mLoadingView.showEmptyView(true);
            mLoadingView.setEmptyListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    requestDatas();
                }
            });
            return;
        }
        pos = getArguments().getInt("pos") -1;
        num = getArguments().getInt("num");
        mSort = getArguments().getString("sort");
        buId = getArguments().getString("supplierBusinessId");
        mContractUkid = getArguments().getString("contractUkid");
        selectTag = (List<ContractChooseGoodsBean.ListBean>) getArguments().getSerializable("selectTag");
        ContractChooseGoodsBean dataBean = (ContractChooseGoodsBean) getArguments().getSerializable("data");
        if (dataBean == null || dataBean == null || dataBean.getList() == null || dataBean.getList().size() == 0) {
            mLoadingView.showEmptyView(true);
            mLoadingView.setEmptyListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    requestDatas();
                }
            });
            return;
        }
        mListBean.clear();
        for (int i = pos * num; i < (pos + 1) * num; i++) {
            if (i >= 0 && i < dataBean.getList().size()) {
                mListBean.add(dataBean.getList().get(i));
            }
        }
        initData();


    }

    private void initData() {
            if (mListBean != null && mListBean.size() == 0) {
                mLoadingView.showEmptyView(true);
                mLoadingView.setEmptyListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        requestDatas();
                    }
                });
                return;
            }
        tagBeanList.addAll(mListBean);
        mRvContent.setLayoutManager(new GridLayoutManager(mActivity, 1) {
                @Override
                public boolean canScrollHorizontally() {
                    return false;
                }

                @Override
                public boolean canScrollVertically() {
                    return false;
                }

            });
            final ViewTreeObserver observer = mRvContent.getViewTreeObserver();
            observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    observer.removeGlobalOnLayoutListener(this);
                    if (mAdapter == null) {
                        mAdapter = new ContractChooseGoodsAdapter(mActivity, tagBeanList, mRvContent.getHeight(), mRvContent.getWidth(), num, buId,selectTag);
                    }
                    mRvContent.setAdapter(mAdapter);
                }
            });


    }

    @Override
    protected void init(View view) {
        mRvContent = findView(view, R.id.rv_content);
    }

    @Override
    public void onSuccess(CommonClass commonClass, int what) {

        if (what == 0) {
            if (TextUtils.equals("0", commonClass.getCode())) {
                List<ContractChooseGoodsBean.ListBean> dataBean = JSON.parseArray(commonClass.getData().toString(), ContractChooseGoodsBean.ListBean.class);
                if (dataBean != null && dataBean.size() != 0) {
                    tagBeanList.addAll(dataBean);
                }
                mRvContent.setLayoutManager(new GridLayoutManager(mActivity, 1) {
                    @Override
                    public boolean canScrollHorizontally() {
                        return false;
                    }

                    @Override
                    public boolean canScrollVertically() {
                        return false;
                    }

                });
                final ViewTreeObserver observer = mRvContent.getViewTreeObserver();
                observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        observer.removeGlobalOnLayoutListener(this);
                        if (mAdapter == null) {
                            mAdapter = new ContractChooseGoodsAdapter(mActivity, tagBeanList, mRvContent.getHeight(), mRvContent.getWidth(), num, buId,selectTag);
                        }
                        mRvContent.setAdapter(mAdapter);
                    }
                });

            } else {
                toast(commonClass.getMsg());
            }
        }
    }

    @Override
    public void onReLoad(int code) {

    }

//    private void requestData(Bundle b) {
//        Map<String, Object> map = new HashMap<>();
//        map.put("supplierBusinessId",buId); //   业务单位ID(必填)
//        map.put("page", b.getInt("pos") + ""); //
//        map.put("contractUkid", mContractUkid); //  发布ukid
//        map.put("rsName", b.getString("rsName")); // 资源名称
//        map.put("size", b.getInt("num") + ""); //
//        map.put("sort", b.getString("sort")); //     排序(DESC,ASC)
//        httpPost(ContractConstant.RESOURCEITEMS, map, 0, true, "");
//    }

}
